<?php
class Kal_Timetables_PostController extends Mage_Core_Controller_Front_Action
{
	public function preDispatch()
    {
        parent::preDispatch();
    }
	
    public function viewAction()
    {	
		$identifier = $this->getRequest()->getParam('identifier', $this->getRequest()->getParam('id', false));
        if (!Mage::helper('timetables/post')->renderPage($this, $identifier)) {
			$this->_forward('NoRoute');
        }

    }
	
	public function noRouteAction($coreRoute = null)
    {
        $this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
        $this->getResponse()->setHeader('Status','404 File not found');

        $pageId = Mage::getStoreConfig('web/default/cms_no_route');
        if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
            $this->_forward('defaultNoRoute');
        }
    }
	
	public function postAction()
    {
       if ($data = $this->getRequest()->getPost()) {
			$model = Mage::getModel('timetables/comment');		
			$model->setData($data);
			
			if (!Mage::getStoreConfig('timetables/comments/enabled'))
			{
				Mage::getSingleton('customer/session')->addError(Mage::helper('timetables')->__('Comments are not enabled.'));
                $this->_redirectReferer();
                return;
			}

			if (!Mage::getSingleton('customer/session')->isLoggedIn() && Mage::getStoreConfig('timetables/comments/login'))
			{
				Mage::getSingleton('customer/session')->addError(Mage::helper('timetables')->__('You must be logged in to comment.'));
                $this->_redirectReferer();
                return;
			}
			else if (Mage::getSingleton('customer/session')->isLoggedIn() && Mage::getStoreConfig('timetables/comments/login'))
			{
				$model->setUser(Mage::helper('timetables')->getUserName());
				$model->setEmail(Mage::helper('timetables')->getUserEmail());
			}
			
			
			try 
			{
				$model->setCreatedTime(now());
				$model->setComment(htmlspecialchars($model->getComment(), ENT_QUOTES));
				if (Mage::getStoreConfig('timetables/comments/approval'))
				{
					$model->setStatus(2);
					Mage::getSingleton('customer/session')->addSuccess(Mage::helper('timetables')->__('Your comment has been submitted.'));
				}
				else if (Mage::getSingleton('customer/session')->isLoggedIn() && Mage::getStoreConfig('timetables/comments/loginauto'))
				{
					$model->setStatus(2);
					Mage::getSingleton('customer/session')->addSuccess(Mage::helper('timetables')->__('Your comment has been submitted.'));
				}
				else
				{
					$model->setStatus(1);
					Mage::getSingleton('customer/session')->addSuccess(Mage::helper('timetables')->__('Your comment has been submitted and is awaiting approval.'));
				}
				$model->save();
				
				$comment_id = $model->getCommentId();
                
            } catch (Exception $e) {
                Mage::getSingleton('customer/session')->addError(Mage::helper('timetables')->__('Unable to submit your comment. Please, try again later'));
                $this->_redirectReferer();
                return;
            }
			
			if (Mage::getStoreConfig('timetables/comments/recipient_email') != null && $model->getStatus() == 1 && isset($comment_id))
			{
				$translate = Mage::getSingleton('core/translate');
				/* @var $translate Mage_Core_Model_Translate */
				$translate->setTranslateInline(false);
				try {
					$data["url"] = Mage::getUrl('timetables/manage_comment/edit/id/' . $comment_id);
					$postObject = new Varien_Object();
					$postObject->setData($data);
					$mailTemplate = Mage::getModel('core/email_template');
					/* @var $mailTemplate Mage_Core_Model_Email_Template */
					$mailTemplate->setDesignConfig(array('area' => 'frontend'))
						->sendTransactional(
							Mage::getStoreConfig('timetables/comments/email_template'),
							Mage::getStoreConfig('timetables/comments/sender_email_identity'),
							Mage::getStoreConfig('timetables/comments/recipient_email'),
							null,
							array('data' => $postObject)
						);
					$translate->setTranslateInline(true);
				} catch (Exception $e) {
					$translate->setTranslateInline(true);
				}
			}
			
			$this->_redirectReferer();
            return;
        } else {
            $this->_redirectReferer();
        }
	}
}